package com.study.exam.service;

import com.study.exam.mapper.UserMapper;
import com.study.exam.pojo.User;
import com.study.exam.vo.UserUpdateVO;
import com.study.exam.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Resource
    private UserMapper userMapper;

    public Map<String, Object> selectUsers(UserVO userVO) {
        System.out.println("--------------------------------------------------------------------");
        System.out.println(userVO.getPage());
        System.out.println(userVO.getLimit());
        System.out.println(userVO.getUsername());
        System.out.println(userVO.getDepartment_id());
        System.out.println("--------------------------------------------------------------------");
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询用户列表成功");
        map.put("data", userMapper.selectUsers(userVO));
        return map;
    }

    public Map<String, Object> insert(UserVO userVO) {
        User user = new User();
        BeanUtils.copyProperties(userVO, user);   // 把前端传递的值对象数据，自动封装到pojo对象中去(反射机制)

        // 手动修改用户密码为md5值
        String md5 = DigestUtils.md5DigestAsHex(user.getPassword().getBytes(StandardCharsets.UTF_8));
        user.setPassword(md5);

        userMapper.insert(user);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增用户成功");
        map.put("data", user);
        return map;
    }

    public Map<String, Object> login(UserVO userVO) {
        User user = new User();
        BeanUtils.copyProperties(userVO, user);   // 把前端传递的值对象数据，自动封装到pojo对象中去(反射机制)

        user = userMapper.login(user);
        /*
        1.user对象如果为null  代表用户名没有找到
        2.user对象不为null  但是密码对不上
        3.user对象不为null  并且密码也对上
         */
        Map<String, Object> map = new HashMap<>();
        if(user == null) {
            map.put("code", 60001);
            map.put("msg", "对不起，用户名不存在！");
        }
        else {
            String sql_psd = user.getPassword();
            String user_psd = DigestUtils.md5DigestAsHex(userVO.getPassword().getBytes(StandardCharsets.UTF_8));
            if(sql_psd.equals(user_psd)) {
                map.put("code", 200);
                map.put("msg", "登录成功！");
            }
            else {
                map.put("code", 60002);
                map.put("msg", "密码错误！");
            }
        }

        return map;
    }


    /**
     * 根据指定的主键id删除一个用户记录
     * @param id  用户的主键id
     * @return    返回删除的状态和信息
     */
    public Map<String, Object> deleteById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        userMapper.deleteById(id);
        map.put("code", 200);
        map.put("msg", "删除用户成功");
        return map;
    }

    public Map<String, Object> updateById(UserUpdateVO userUpdateVO) {
        Map<String, Object> map = new HashMap<>();
        userMapper.updateById(userUpdateVO);
        map.put("code", 200);
        map.put("msg", "更新用户成功");
        return map;
    }

}
